/*
 *  node.cc
 *  Untitled
 *
 *  Created by Ben Nolan on 2/11/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#include "node.h"
#include "vector.h"
#include "errors.h"

#include <sstream>
#include "tinyxml/ticpp.h"

namespace Wonderland{

Node::Node(ticpp::Element* el){
	loadFromXml(el);
}

Node::Node(std::string xml){
	ticpp::Document doc;

	try{
		doc.Parse(xml.c_str());
		loadFromXml(doc.FirstChildElement());
	}catch(ticpp::Exception& ex){
		throw Wonderland::Errors::BadSerialization();
	}
}

void Node::loadFromXml(ticpp::Element* el){
	guid_ = Guid(el->GetAttribute("guid"));
	owner_ = Wid(el->GetAttribute("owner"));
	agent_ = Wid(el->GetAttribute("agent"));

	position_ = Vector(el->GetAttribute("position"));
	scale_ = Vector(el->GetAttribute("scale"));
	rotation_ = Quaternion(el->GetAttribute("rotation"));
}


std::string Node::toString(){
	std::stringstream xml;

	ticpp::Element* el = toXml();
	xml << *el << std::endl;
	delete el;

	return xml.str();
}

ticpp::Element* Node::toXml(){
    ticpp::Element* node = new ticpp::Element("node");
	
    node->SetAttribute("guid", guid_.toString());
    node->SetAttribute("owner", owner_.toString());
    node->SetAttribute("agent", agent_.toString());

    node->SetAttribute("position", position_.toString());
    node->SetAttribute("scale", scale_.toString());
    node->SetAttribute("rotation", rotation_.toString());

	return node;
}

}